package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.mapper.WeiboMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.vo.CommentVO;
import cn.tedu.weibo.pojo.vo.UserVO;
import cn.tedu.weibo.pojo.vo.WeiboListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class CommentController {
    @Autowired(required = false)
    CommentMapper commentMapper;

    @RequestMapping("selectByWeiboId")
    public List<CommentVO> selectByWeiboId(int id) {
        return commentMapper.selectByWeiboId(id);
    }

    @RequestMapping("/comment/insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("user");
        if (user == null) {
            return 2;
        }
        Comment c = new Comment();
        BeanUtils.copyProperties(commentDTO, c);
        c.setUserId(user.getId());
        System.out.println(c);
        commentMapper.insert(c);
        return 1;
    }
}
